
package com.work.mvc.view.window;

import com.vaadin.server.StreamResource;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.Reindeer;
import com.work.constants.view.*;
import com.work.mvc.*;
import com.work.pdf.*;
import com.work.reports.ReportType;

public class ReportWindowView extends Window implements View {

	private static final long serialVersionUID = -987440870493135719L;

	private VerticalLayout content = new VerticalLayout();

	private String title;
	private PdfGenerator generator;

	public ReportWindowView(ReportType reportType, PdfGenerator pdfGenerator) {
		this.title = reportType.getTitle();
		this.generator = pdfGenerator;
		init();
	}

	@Override
	public void init()
	{
		generator.generatePdf();
		
		content.setHeight("99%");
		content.addComponent(createContent());

		this.setCaption(title);
		this.setModal(true);
		this.setStyleName(Reindeer.WINDOW_BLACK);
		this.setWidth(ComponentsSize.WORKDAY_WINDOW_WIDTH);
		this.setHeight(ComponentsSize.WORKDAY_WINDOW_HEIGHT);
		this.setClosable(true);
		this.setResizable(true);
		this.setDraggable(true);
		this.center();
		this.setContent(content);
	}

	@Override
	public Controller getController()
	{
		return null;
	}

	private Embedded createContent()
	{
		StreamResource resource = new StreamResource(new PdfStreamSource(generator.getByteArrayOutputStream()), "test.pdf");
		resource.setMIMEType(ComponentsSources.MIME_TYPE_PDF);
		Embedded embedded = new Embedded();
		embedded.setSizeFull();
		embedded.setType(Embedded.TYPE_BROWSER);
		embedded.setSource(resource);

		return embedded;
	}
}
